package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException_自定义异常;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.DishFlavor_菜品口味;
import com.itheima.reggie.pojo.Dish_菜品;
import com.itheima.reggie.pojo.SetmealDish_套餐与菜品;
import com.itheima.reggie.pojo.Setmeal_套餐;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: cl
 * @Date: 2022/4/3 - 04 - 03
 * @Description:
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal_套餐>
        implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增套餐并更新套餐菜品关系表
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {

        //1、保存套餐信息
        this.save(setmealDto);

        //2、套餐菜品关系表更新
        List<SetmealDish_套餐与菜品> setmealDishes = setmealDto.getSetmealDishes();
         setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //3、操作更新套餐菜品关系表
        setmealDishService.saveBatch(setmealDishes);
    }


    /**
     * 通过id查询套餐并复制其菜品
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithFlavor(long id) {
        //查询套餐
        Setmeal_套餐 setmeal = this.getById(id);
        //创建setmealDto复制并记录菜品
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal, setmealDto);

        //查询菜品

        LambdaQueryWrapper<SetmealDish_套餐与菜品> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish_套餐与菜品::getSetmealId, id);
        List<SetmealDish_套餐与菜品> setmealDish = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(setmealDish);

        return setmealDto;

    }

    /**
     * 修改套餐并修改套餐关系表中的菜品
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {

        //修改套餐
        this.updateById(setmealDto);

        //更新菜品，因为不同菜品对应同一个setmealId 所以不能通过ID修改
        //办法：先删除对应的菜品，再重新添加

        //删除菜品
        setmealDishService.remove(
                Wrappers.<SetmealDish_套餐与菜品>lambdaQuery()
                        .eq(setmealDto.getId() != null, SetmealDish_套餐与菜品::getSetmealId, setmealDto.getId())
        );

        //添加菜品(因为菜品实体类中还没有记录setmealId，先把id设置上)
        List<SetmealDish_套餐与菜品> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //添加菜品
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal_套餐> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal_套餐::getId,ids);
        queryWrapper.eq(Setmeal_套餐::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException_自定义异常("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish_套餐与菜品> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish_套餐与菜品::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
    }
}
